$(document).ready(
    $("#login_btn").click(function(){
        var user_name = $.trim($("#user_name").val());
        var password = $.trim($("#password").val());
        if(user_name.length == 0 || password.length == 0){
            alert("请输入账号和密码！");
            return false;
        }
        var validateCode = $.trim($("#validateCode").val());
        if(validateCode.length != 4){
            alert("请输入有效的验证码！");
            return false;
        }
        $.ajax({
            type: "POST",
            url:"/validateCode/validate",
            data:{"code":validateCode},
            async:false,
            error: function(request) {
                alert("网络异常，提交数据失败，请重新提交！");
                changeValidateCode(document.getElementById("validateImg"));
                return false;
            },
            success: function(data) {
                if(data == 'fail'){
                    alert("验证码输入有误请重新输入！");
                    changeValidateCode(document.getElementById("validateImg"));
                }else{
                    var pData = {};
                    pData.username = user_name;
                    pData.password = password;
                    $.ajax({
                        type: "POST",
                        url:"/login/dologin",
                        data:pData,
                        async:false,
                        dataType: 'json',
                        error: function(request) {
                            alert("网络异常，提交数据失败，请重新提交！");
                        },
                        success: function(res) {
                            if(res.code == 0){
                                alert("登录成功!");
                                location.reload();
                            }else{
                                alert(res.msg != "" ? res.msg : "网络异常，提交数据失败，请重新提交！");
                            }
                        }
                    });
                }
            }
        });
    })
);